package org.lecric.wind44.client;

import java.util.Date;
import java.util.List;

import org.lecric.wind44.client.model.Post;
import org.lecric.wind44.client.service.MobileService;
import org.lecric.wind44.client.service.MobileServiceAsync;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Mobile implements EntryPoint
{
	private static final Messages MESSAGES=GWT.create(Messages.class);

	public static final MobileServiceAsync MOBILE_SERVICE = GWT.create(MobileService.class);

	private Timer loadingTimer;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		loadingTimer = new Timer() {
			private int i=0;
			@Override
			public void run() {
				i++;
				String text="Loading";
				for(int j=0; j <= i%3 ; j++)
				{
					text+='.';
				}
				Document.get().getElementById("loading").setInnerText(text);
			}
		};
		loadingTimer.run();
		loadingTimer.scheduleRepeating(1000);
		loadingTimer.cancel();
		Document.get().getElementById("loading").setInnerText("");
		final VerticalPanel vpanel=new VerticalPanel();
		Button addButton = new Button("Add Post");
		addButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Post post=new Post();
				post.setMessage("Blabla");
				post.setUser("lecric");
				post.setSpot("Pont Mahe");
				post.setDate(new Date());
				MOBILE_SERVICE.post(post, new AsyncCallback<Post>() {
					@Override
					public void onFailure(Throwable caught) { }

					@Override
					public void onSuccess(Post result) {
						// TODO Auto-generated method stub
					}
				});
			}
		});
		vpanel.add(addButton);
		Button clearButton = new Button("Clear Posts");
		clearButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				MOBILE_SERVICE.clearPosts(new AsyncCallback<Void>() {
					@Override
					public void onFailure(Throwable caught) { }
					@Override
					public void onSuccess(Void v) { }
				});
			}
		});
		vpanel.add(clearButton);
		MOBILE_SERVICE.getPosts(new AsyncCallback<List<Post>>() {
			public void onFailure(Throwable caught) {};
			public void onSuccess(List<Post> posts) {
				for(Post post: posts)
				{
					vpanel.add(createTitle(MESSAGES.sent(post.getDate(), post.getSpot())));
					vpanel.add(createMessage(post.getUser() + " a ecrit:<br>" + post.getMessage()));
				}
			};
		});
		RootPanel.get("main").add(vpanel);
	}

	private HTML createMessage(String string) {
		return new HTML(string);
	}

	private HTML createTitle(String string) {
		return new HTML("<h1>"+string+"</h1>");
	}
	
	
}
